<?php
/**
 * User: Pan
 * Date: 2018/5/10
 * Time: 10:12
 */

namespace app\admin\controller;

use app\admin\model\Banner;
use app\admin\model\BannerItem;
use think\Request;

class BannerController extends BaseController
{
    /*
     * Banner位列表页面
     */
    public function index()
    {
        //多种条件查询显示
        $lists = Banner::getAllBanner(5,$this->request->get());
        $this->assign('lists',$lists);
        return $this->fetch();
    }

    /*
     * Banner位添加页面
     */
    public function add(Request $request)
    {
        if($request->isAjax()){
            //post传递为添加操作，进行验证，
            $data = $request->only(['name','description']);
            $result = $this->validate($data,[
                'name' => 'require|chsDash|min:1',
            ]);
            //如果验证失败，返回错误信息
            if(true !== $result){
                return ajaxMsg(0,$result);
            }
            //添加处理
            if(Banner::create($data)){
                return ajaxMsg(1,'添加数据成功');
            }else{
                return ajaxMsg(0,'添加数据异常');
            }
        }

        return $this->fetch();

    }

    //删除banner图
    public function del($id)
    {
        if(Banner::destroy($id)){
            return 1;
        }else{
            return 0;
        }
    }
    //编辑操作
    public function edi($id)
    {
        //获取当前banner模型
        $banner = Banner::get($id);
        //post为更新数据，验证，处理，返回数据
        if($this->request->isAjax()){
            $data = $this->request->only(['name','id','description']);
            $result = $this->validate($data,[
                'name' => 'require',
                'id' => 'require|number',
            ]);
            //如果验证失败，返回错误信息
            if(true !== $result){
                return ajaxMsg(0,$result);
            }
            $banner = new Banner();
            if($banner->isUpdate(true)->save($data)){
                return ajaxMsg(1,'更新数据成功');
            }else{
                return ajaxMsg(0,'更新数据异常');
            }
        }
        $this->assign('banner',$banner);
        return $this->fetch();
    }

    /*
     * Banner位下属内容列表
     */
    public function bannerlist()
    {
        $id = input('id');
        $lists = Banner::with(['item'=>function($query){
            $query->order('create_time','desc');
        }])->find($id);
        $this->assign('lists',$lists);
        return $this->fetch();
    }

    //添加对应banner图
    public function additem(Request $request)
    {
        if($request->isPost()){
            $data = $request->param();
            if($this->request->file('img')){
                $uploadFile = uploadFile('img',null,null,'uploads/banner_item');
                if($uploadFile){
                    $data['img'] = $uploadFile['origin'];
                    $data['img_origin'] = $uploadFile['tag'];
                }
            }
            $model = new BannerItem();
            if($model->allowField(true)->isUpdate(false)->save($data)){
                return ['error'=>0];
            }else{
                return ['error'=>1,'msg'=>'添加失败,联想管理员!'];
            }
        }
        $banner_id = input('id');
        $banners = Banner::all();
        $this->assign([
            'banner' => Banner::get($banner_id),
            'banners' => $banners,
        ]);
        return $this->fetch();
    }

    /*
     * 编辑banner图
     */
    public function edit_item(Request $request)
    {
        if($request->isPost()){
            $model = new BannerItem();
            $data = $request->param();
            if($this->request->file('img')){
                $old_img = $model->field('img,img_origin')->find($data['id']);
                uploadDeleteFile([[$old_img->getData('img'),$old_img->getData('img_origin')]]);
                $uploadFile = uploadFile('img',null,null,'uploads/banner_item');
                if($uploadFile){
                    $data['img'] = $uploadFile['origin'];
                    $data['img_origin'] = $uploadFile['tag'];
                }
            }
            if($model->allowField(true)->isUpdate(true)->save($data)){
                return ['error'=>0];
            }else{
                return ['error'=>1,'msg'=>'添加失败,联想管理员!'];
            }
        }
        $item_id = $request->param('id');
        $banner = Banner::all();
        $this->assign(['item' => BannerItem::get($item_id),'banner'=>$banner]);
        return $this->fetch();
    }

    /*
     * 删除banner位的图片和内容
     */
    public function delitem($id)
    {
        if(BannerItem::destroy($id)){
            return 1;
        }else{
            return 0;
        }
    }

    /*
 * 批量删除用户操作
 */
    public function delall()
    {
        $id = input('post.id/a');
        if(Banner::destroy($id))
        {
            return ajaxMsg(1,'删除成功');
        }else{
            return ajaxMsg(0,'删除失败');
        }

    }

    /*
     * 批量删除Banner item
     */
    public function delallitem()
    {
        $id = input('post.id/a');
        if(BannerItem::destroy($id))
        {
            return ajaxMsg(1,'删除成功');
        }else{
            return ajaxMsg(0,'删除失败');
        }
    }

    /*
     * 修改banner图是否启用
     */
    public function is_use($id)
    {
        $item = BannerItem::get($id);
        //当前banner图状态如果是开启则为关闭，是关闭则为开启
        $item->is_use == 1 ? $item->is_use = 0:$item->is_use = 1;
        if($item->save()){
            return 1;
        }else{
            return 0;
        }
    }

}